﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gmis.IDAL;
using Gmis.DBUtility;

namespace Gmis.SqlServerDAL
{
    /// <summary>
    /// 数据访问类:ArtelBuy
    /// </summary>
    public partial class ArtelBuy : IArtelBuy
    {
        public ArtelBuy()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ArtelBuy");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Gmis.Model.ArtelBuy model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ArtelBuy(");
            strSql.Append("sId,sArtelId,sNumber,dReg,sContact,sBrand,sName,sSpec,nCount,sUnit,nPrice,nTotal,dDelivery,sPurchaser,sProducer)");
            strSql.Append(" values (");
            strSql.Append("@sId,@sArtelId,@sNumber,@dReg,@sContact,@sBrand,@sName,@sSpec,@nCount,@sUnit,@nPrice,@nTotal,@dDelivery,@sPurchaser,@sProducer)");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50),
					new SqlParameter("@sArtelId", SqlDbType.VarChar,50),
					new SqlParameter("@sNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@dReg", SqlDbType.DateTime),
					new SqlParameter("@sContact", SqlDbType.NVarChar,50),
					new SqlParameter("@sBrand", SqlDbType.NVarChar,50),
					new SqlParameter("@sName", SqlDbType.NVarChar,128),
					new SqlParameter("@sSpec", SqlDbType.NVarChar,128),
					new SqlParameter("@nCount", SqlDbType.Decimal,9),
					new SqlParameter("@sUnit", SqlDbType.NVarChar,50),
					new SqlParameter("@nPrice", SqlDbType.Decimal,9),
					new SqlParameter("@nTotal", SqlDbType.Decimal,9),
					new SqlParameter("@dDelivery", SqlDbType.DateTime),
					new SqlParameter("@sPurchaser", SqlDbType.NVarChar,128),
					new SqlParameter("@sProducer", SqlDbType.NVarChar,128)};
            parameters[0].Value = model.sId;
            parameters[1].Value = model.sArtelId;
            parameters[2].Value = model.sNumber;
            parameters[3].Value = model.dReg;
            parameters[4].Value = model.sContact;
            parameters[5].Value = model.sBrand;
            parameters[6].Value = model.sName;
            parameters[7].Value = model.sSpec;
            parameters[8].Value = model.nCount;
            parameters[9].Value = model.sUnit;
            parameters[10].Value = model.nPrice;
            parameters[11].Value = model.nTotal;
            parameters[12].Value = model.dDelivery;
            parameters[13].Value = model.sPurchaser;
            parameters[14].Value = model.sProducer;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gmis.Model.ArtelBuy model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ArtelBuy set ");
            strSql.Append("sArtelId=@sArtelId,");
            strSql.Append("sNumber=@sNumber,");
            strSql.Append("dReg=@dReg,");
            strSql.Append("sContact=@sContact,");
            strSql.Append("sBrand=@sBrand,");
            strSql.Append("sName=@sName,");
            strSql.Append("sSpec=@sSpec,");
            strSql.Append("nCount=@nCount,");
            strSql.Append("sUnit=@sUnit,");
            strSql.Append("nPrice=@nPrice,");
            strSql.Append("nTotal=@nTotal,");
            strSql.Append("dDelivery=@dDelivery,");
            strSql.Append("sPurchaser=@sPurchaser,");
            strSql.Append("sProducer=@sProducer");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sArtelId", SqlDbType.VarChar,50),
					new SqlParameter("@sNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@dReg", SqlDbType.DateTime),
					new SqlParameter("@sContact", SqlDbType.NVarChar,50),
					new SqlParameter("@sBrand", SqlDbType.NVarChar,50),
					new SqlParameter("@sName", SqlDbType.NVarChar,128),
					new SqlParameter("@sSpec", SqlDbType.NVarChar,128),
					new SqlParameter("@nCount", SqlDbType.Decimal,9),
					new SqlParameter("@sUnit", SqlDbType.NVarChar,50),
					new SqlParameter("@nPrice", SqlDbType.Decimal,9),
					new SqlParameter("@nTotal", SqlDbType.Decimal,9),
					new SqlParameter("@dDelivery", SqlDbType.DateTime),
					new SqlParameter("@sPurchaser", SqlDbType.NVarChar,128),
					new SqlParameter("@sProducer", SqlDbType.NVarChar,128),
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = model.sArtelId;
            parameters[1].Value = model.sNumber;
            parameters[2].Value = model.dReg;
            parameters[3].Value = model.sContact;
            parameters[4].Value = model.sBrand;
            parameters[5].Value = model.sName;
            parameters[6].Value = model.sSpec;
            parameters[7].Value = model.nCount;
            parameters[8].Value = model.sUnit;
            parameters[9].Value = model.nPrice;
            parameters[10].Value = model.nTotal;
            parameters[11].Value = model.dDelivery;
            parameters[12].Value = model.sPurchaser;
            parameters[13].Value = model.sProducer;
            parameters[14].Value = model.sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string sId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ArtelBuy ");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string sIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ArtelBuy ");
            strSql.Append(" where sId in (" + sIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gmis.Model.ArtelBuy GetModel(string sId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 sId,sArtelId,sNumber,dReg,sContact,sBrand,sName,sSpec,nCount,sUnit,nPrice,nTotal,dDelivery,sPurchaser,sProducer from ArtelBuy ");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            Gmis.Model.ArtelBuy model = new Gmis.Model.ArtelBuy();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sId"] != null && ds.Tables[0].Rows[0]["sId"].ToString() != "")
                {
                    model.sId = ds.Tables[0].Rows[0]["sId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sArtelId"] != null && ds.Tables[0].Rows[0]["sArtelId"].ToString() != "")
                {
                    model.sArtelId = ds.Tables[0].Rows[0]["sArtelId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sNumber"] != null && ds.Tables[0].Rows[0]["sNumber"].ToString() != "")
                {
                    model.sNumber = ds.Tables[0].Rows[0]["sNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["dReg"] != null && ds.Tables[0].Rows[0]["dReg"].ToString() != "")
                {
                    model.dReg = DateTime.Parse(ds.Tables[0].Rows[0]["dReg"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sContact"] != null && ds.Tables[0].Rows[0]["sContact"].ToString() != "")
                {
                    model.sContact = ds.Tables[0].Rows[0]["sContact"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sBrand"] != null && ds.Tables[0].Rows[0]["sBrand"].ToString() != "")
                {
                    model.sBrand = ds.Tables[0].Rows[0]["sBrand"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sName"] != null && ds.Tables[0].Rows[0]["sName"].ToString() != "")
                {
                    model.sName = ds.Tables[0].Rows[0]["sName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sSpec"] != null && ds.Tables[0].Rows[0]["sSpec"].ToString() != "")
                {
                    model.sSpec = ds.Tables[0].Rows[0]["sSpec"].ToString();
                }
                if (ds.Tables[0].Rows[0]["nCount"] != null && ds.Tables[0].Rows[0]["nCount"].ToString() != "")
                {
                    model.nCount = decimal.Parse(ds.Tables[0].Rows[0]["nCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sUnit"] != null && ds.Tables[0].Rows[0]["sUnit"].ToString() != "")
                {
                    model.sUnit = ds.Tables[0].Rows[0]["sUnit"].ToString();
                }
                if (ds.Tables[0].Rows[0]["nPrice"] != null && ds.Tables[0].Rows[0]["nPrice"].ToString() != "")
                {
                    model.nPrice = decimal.Parse(ds.Tables[0].Rows[0]["nPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["nTotal"] != null && ds.Tables[0].Rows[0]["nTotal"].ToString() != "")
                {
                    model.nTotal = decimal.Parse(ds.Tables[0].Rows[0]["nTotal"].ToString());
                }
                if (ds.Tables[0].Rows[0]["dDelivery"] != null && ds.Tables[0].Rows[0]["dDelivery"].ToString() != "")
                {
                    model.dDelivery = DateTime.Parse(ds.Tables[0].Rows[0]["dDelivery"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sPurchaser"] != null && ds.Tables[0].Rows[0]["sPurchaser"].ToString() != "")
                {
                    model.sPurchaser = ds.Tables[0].Rows[0]["sPurchaser"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sProducer"] != null && ds.Tables[0].Rows[0]["sProducer"].ToString() != "")
                {
                    model.sProducer = ds.Tables[0].Rows[0]["sProducer"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sId,sArtelId,sNumber,dReg,sContact,sBrand,sName,sSpec,nCount,sUnit,nPrice,nTotal,dDelivery,sPurchaser,sProducer ");
            strSql.Append(" FROM ArtelBuy ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" sId,sArtelId,sNumber,dReg,sContact,sBrand,sName,sSpec,nCount,sUnit,nPrice,nTotal,dDelivery,sPurchaser,sProducer ");
            strSql.Append(" FROM ArtelBuy ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "ArtelBuy";
            parameters[1].Value = "sId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

